﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Web;
using UnionManager.Infrastructure.Config;

namespace UnionManager.Infrastructure
{
    public static class PresentationUtils
    {
        public static bool IsRole(this HttpRequestBase request, IPrincipal user, int role)
        {
            return request.IsAuthenticated && ((EnhancedPrincipal)user).Data.LoaiTaiKhoan == role;
        }
        //public static bool IsAdmin(this HttpRequestBase request, IPrincipal user)
        //{
        //    return request.IsAuthenticated && ((EnhancedPrincipal)user).Data.MaQuyen == "Admin";
        //}
        //public static bool IsMod(this HttpRequestBase request, IPrincipal user)
        //{
        //    return request.IsAuthenticated && (((EnhancedPrincipal)user).Data.MaQuyen == "NhanVien" || ((EnhancedPrincipal)user).Data.MaQuyen == "Admin");
        //}
        //public static bool IsMember(this HttpRequestBase request, IPrincipal user)
        //{
        //    return request.IsAuthenticated && (((EnhancedPrincipal)user).Data.MaQuyen == "HKTT" || ((EnhancedPrincipal)user).Data.MaQuyen == "NhanVien" || ((EnhancedPrincipal)user).Data.MaQuyen == "Admin");
        //}

        public static string UrlEncode(this string text)
        {
            return HttpUtility.UrlEncode(text);
        }

        public static string UrlDecode(this string text)
        {
            return HttpUtility.UrlDecode(text);
        }

        public static string Encrypt(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return string.Empty;
            var md5 = new MD5CryptoServiceProvider();
            byte[] valueArray = Encoding.ASCII.GetBytes(value);
            valueArray = md5.ComputeHash(valueArray);
            var sb = new StringBuilder();
            for (int i = 0; i < valueArray.Length; i++)
                sb.Append(valueArray[i].ToString("x2").ToLower());
            return sb.ToString();
        }
    }
}